import matplotlib.pyplot as plt

def parse_data(filename):
    data = {}
    with open(filename) as f:
        current_struct = None
        for line in f:
            line = line.strip()
            if not line:
                continue
            if line[0].isalpha():  # Data structure name
                current_struct = line
                data[current_struct] = {'threads': [], 'times': []}
            else:  # Thread count and time
                parts = line.split()
                data[current_struct]['threads'].append(int(parts[0]))
                data[current_struct]['times'].append(float(parts[1]))
    return data

def plot_performance(data):
    plt.figure(figsize=(12, 8))
    plt.subplots_adjust(right=0.75)  # Add right margin for legend
    markers = ['o', 's', '^', 'D', 'v', 'p', '*', 'h', 'H', '+']  # Markers for 10 structures
    colors = ['b', 'g', 'r', 'm', 'c', 'y', 'k', 'tab:orange', 'tab:purple', 'tab:brown']  # Colors for 10 structures
    
    for i, (struct, values) in enumerate(data.items()):
        plt.plot(values['threads'], values['times'], 
                marker=markers[i], color=colors[i], 
                label=struct, linewidth=2)
    
    plt.xlabel('Thread Count', fontsize=12)
    plt.ylabel('Execution Time (ns)', fontsize=12)
    plt.title('Concurrent List Performance Comparison', fontsize=14)
    plt.legend(fontsize=9, bbox_to_anchor=(1.02, 1), loc='upper left')
    plt.tight_layout()  # Automatically adjust subplot params
    plt.grid(True, linestyle='--', alpha=0.7)
    plt.xticks([1, 2, 4, 8, 16, 32])
    plt.savefig('performance_comparison.png')
    plt.show()

if __name__ == "__main__":
    data = parse_data("test.txt")
    plot_performance(data)
